﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Collections;

namespace Universal
{
    public class Caches
    {
        public Caches()
        {
        }
        private static object ThisLock = new object();

        //插入"一键多值"方法
        //***其中 StorageType是一个Enum,里面存有展示模块
        //这个枚举如下:
        #region 方法
        public static void InsertCommonInfWithBranch(string strBranchID)
        {
            //建立回调委托的一个实例
            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
            // if (strName == "Exhibition")
            //  {

            BLL.View.Exhibition bll = new BLL.View.Exhibition();
            string strWhere = string.Format(" BRANCH_ID='{0}'", strBranchID);
            List<Model.View.Exhibition> listModle = bll.GetListArray(strWhere);
            string strName = "Exhibition_" + strBranchID;
            HttpContext.Current.Cache.Insert(strName, listModle, null,
               Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1), CacheItemPriority.High, callBack);
            //  }
        }
        //读取“一键多值”Identify的值
        public static List<Model.View.Exhibition> ReadIdentify(string strIdentify)
        {
            //取出值
            if (HttpContext.Current.Cache[strIdentify] != null)
            {
                List<Model.View.Exhibition> value = null;
                lock (ThisLock)
                {
                    value = HttpContext.Current.Cache[strIdentify] as List<Model.View.Exhibition>;
                }
                if (value != null)
                {
                    return value;
                }

                return null;
            }
            else
            {
                return null;
            }
        }

        //从缓存中获取数据
        public static List<Model.View.Exhibition> getCaceData(string strBranchID)
        {

            List<Model.View.Exhibition> listModle = null;
            string strName = "Exhibition_" + strBranchID;
            listModle = Universal.Caches.ReadIdentify(strName);
            if (listModle == null)
            {
                Universal.Caches.InsertCommonInfWithBranch(strBranchID);
                listModle = Universal.Caches.ReadIdentify(strName);
            }

            return listModle;
        }
        //手动移除“一键一值”对应的值
        public static bool RemoveIdentify(string strIdentify)
        {
            //取出值
            if (HttpContext.Current.Cache[strIdentify] != null)
            {
                HttpContext.Current.Cache.Remove(strIdentify);

            }
            return true;
        }
        //此方法在值失效之前调用，可以用于在失效之前更新数据库，或从数据库重新获取数据
        private static void onRemove(string strIdentify, object objValue, CacheItemRemovedReason reason)
        {


            if (strIdentify.IndexOf("Exhibition") >= 0)
            {
                string strBranchID = strIdentify.Replace("Exhibition_", "");
                InsertCommonInfWithBranch(strBranchID);
            }

        }
        #endregion
    }
}